<?php
namespace App\Http\Controllers\Customer;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Support\Facades\Hash;
class CourseController extends Controller{
	public function __construct()
    {
        $this->middleware('customer');
    }
    public function course()
    {
        return view('customer.course.course');
    }
    public function coursedata(Request $request){
        $key=$request->input('key');
        $state=$request->input('state');
        if (!empty($request->input('start_time'))) {
            $start_time=strtotime($request->input('start_time'));
        }else{
            $start_time=0;
        }
        if (!empty($request->input('end_time'))) {
            $end_time=strtotime($request->input('end_time'));
        }else{
            $end_time=time();
        }
        if($state==0){
            if(!empty($key)){
                $count =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state','<',3)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->count();
            }else{
                $count =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state','<',3)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->count();
            }
        }else{
            if(!empty($key)){
                $count =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state',$state)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->count();
            }else{
                $count =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state',$state)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->count();
            }
        }
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        if($state==0){
            if(!empty($key)){
                $users =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state','<',3)
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->get();
            }else{
                $users =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state','<',3)
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->get();
            }
        }else{
            if(!empty($key)){
                $users =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state',$state)
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->get();
            }else{
                $users =DB::table('course')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('course.time','>',"{$start_time}")
                ->where('course.time','<',"{$end_time}")
                ->where('course.state',$state)
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*', 'teacher.teacher', 'teacher.account')
                ->get();
            }
        }
        foreach ($users as $k=>$item){
            $users[$k]->time =date('Y-m-d H:i:s',$users[$k]->time);
            // $users[$k]->state = $item->state==1 ? '启用' : '禁用';
            // $users[$k]->recommend = $item->recommend==1 ? '推荐' : '未推荐';
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $count,
            'data'  => $users
        ];
        return response()->json($data);
    }
    public function addcourse(Request $request)
    {
        if ($request->isMethod('post')) {
            $t_id=\Auth::guard('customer')->user()->toArray()['id'];
            DB::table('course')->insert(['course' => $request->course,'t_id' =>$t_id,'img' => $request->img, 'price' =>$request->price, 'detail' =>$request->detail, 'state' =>1, 'recommend' =>2, 'time' =>time(), 'uptime' =>time()]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
        return view('customer.course.addcourse');
    }
    public function editcourse(Request $request)
    {
        $res=DB::table('course')->where('id',$request->id)->first();
        $id=$request->id;
        $course=$res->course;
        $img=$res->img;
        $price=$res->price;
        $detail=$res->detail;
        return view('customer.course.editcourse', compact('id','course','img','price','detail'));
    }
    public function editcourseup(Request $request)
    {
        if ($request->isMethod('post')) {
            DB::table('course')->where('id',$request->id)->update(['course' => $request->course, 'img' => $request->img,'price' =>$request->price, 'detail' =>$request->detail,'uptime' =>time()]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
    }
    public function delcourse(Request $request)
    {
            $res=DB::table('course')->where('id',$request->id)->update(['state'=>3]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_off(Request $request)
    {
            $res=DB::table('course')->where('id',$request->id)->update(['state'=>2]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_on(Request $request)
    {
            $res=DB::table('course')->where('id',$request->id)->update(['state'=>1]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function recommend_off(Request $request)
    {
            $res=DB::table('course')->where('id',$request->id)->update(['recommend'=>2]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function recommend_on(Request $request)
    {
            $res=DB::table('course')->where('id',$request->id)->update(['recommend'=>1]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
}